<?php

/**
 *	后台管理系统-管理员
 */

namespace app\admin\controller;

use app\AppApi;
use think\facade\Db;
use think\facade\Request;
use utilTool\Ticket;

class Base
{
    public $adminUid = null;
    public $adminUser = [];
    public $config = [];
    // 服务器地址
    public $domain_name = 'http://admin.cn/';
    public function __construct()
    {
        //允许来自 http://admin.cn 网站的跨域请求 
        header("Access-Control-Allow-Origin:http://localhost:8080");
        // header("Access-Control-Allow-Origin:*");

        date_default_timezone_set('PRC');
        if (empty(Request::isPost())) {
            $this->returnCode(1, '请用post提交');
        }
        $token = Request::header('Authorization');
        if (empty($token)) {
            $token = Request::param('Authorization');
        }
        if (empty($token)) {
            $this->returnCode(90000001);
        }
        $this->adminUid = Ticket::get($token, 'admin');
        $this->adminUser = Db::table('bew_user')->where(['uid' => $this->adminUid])->find();
        if (empty($this->adminUser)) {
            $this->returnCode(90000002);
        }

        # 获取当前链接，查询是否有权限
        $controller = request()->controller();
        $action = request()->action();
        $key = $controller . '/' . $action;

        # 演示站专用
        // if(Request::isPost()){
        //     if(
        //         $key == 'Bew/menusave' || 
        //         $key == 'Bew/menudel' || 
        //         $key == 'Bewadmin/groupsave' || 
        //         $key == 'Bewadmin/groupdel' || 
        //         $key == 'Bewadmin/usersave' || 
        //         $key == 'Bewadmin/userdel' || 
        //         $key == 'Bewtest/testsave' || 
        //         $key == 'Bewtest/testdel' || 
        //         $key == 'Index/upload_img' || 
        //         $key == 'Index/upload_imgs' || 
        //         $key == 'Index/userinfo'
        //     ){
        //         $this->returnCode(1,'演示站，不能操作');
        //     }
        // }
        // 演示站专用
    }
    /**
     * 返回json对象
     */
    protected function returnCode($code, $data = [])
    {
        header('Content-type:application/json');
        if ($code == 0) {
            $arr = array(
                'code' => $code,
                'msg' => '成功',
                'data' => $data
            );
        } else if ($code >= 1 && $code <= 100) {
            $arr = array(
                'code'    =>    $code,
                'msg'    =>    $data
            );
        } else {
            $appapi = new AppApi();
            $arr = array(
                'code' => $code,
                'msg' => $appapi::errorTip($code)
            );
        }
        echo json_encode($arr);
        if ($code != 0) {
            exit;
        }
    }
}
